// 获取应用实例

const app = getApp()
const backgroundAudioManager = wx.getBackgroundAudioManager()
Page({
	data: {
		img_src:app.globalData.img_src,
		video_src:app.globalData.video_src,
		popshow: false,
		show: false,
		show4:true,
		nickname:"",
		userinfo:null,
		clickable:true
		
	},
	onLoad(options){
		console.log(options)
		// return;
		wx.hideHomeButton()
		var uid = options.uid;
		console.log(uid)
		
		if(!uid){
			wx.redirectTo({
				url: '/pages/index/index'
			})
		}
	
		wx.setStorageSync('invite_id', uid)
		this.login()
		
		this.getinfo(uid);
		
		console.log(app.globalData)
		
	},
	onReady(){
		setTimeout(function(){
			backgroundAudioManager.title = 'Happy Moments'
			backgroundAudioManager.src = 'https://ikea.uglobal.com.cn/guerlainmemberday/HappyMoments.mp3'
		},3000)
	
		
	},
	onShow(){
		var that = this;
		wx.startGyroscope();
		wx.onGyroscopeChange(function(e){
			if (e.x > 1 && e.y > 1) {
				wx.offGyroscopeChange();
				wx.stopGyroscope();
				that.yaoyiyao();
      }
		})
	},
	getinfo(uid){
		console.log(uid);
		var that = this
		wx.request({
			url: app.globalData.apiurl+'access.php?a=userInfo', 
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			data: {
				openid:uid
			},
			dataType:"json",
			method:"POST",
			success (res) {
				// console.log(res.data.data.member_level != "帝王黑卡");
				// return;
					if(res.data.status == 1){
						
						if(res.data.data.member_level != "帝王黑卡"){

							wx.redirectTo({
								url: '/pages/index/index'
							})
						}
						
						if(res.data.data.fans_nickname){
							that.setData({
								nickname:res.data.data.nickname
							})
						}
						
					}else{
						wx.showToast({
							title: res.data.info,
							icon:'none',
							duration:1500
						})
						wx.redirectTo({
							url: '/pages/index/index'
						})
					}
			}
		})
	},
	login(){
		var that = this;
		wx.login({
     
      success: res => {
        console.log(res.code);
        wx.request({
          url: app.globalData.apiurl+'code2session.php', 
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data: {
            code: res.code
          },
          dataType:"json",
          method:"POST",
          success (res) {
              if(res.data.status == 1){
								var invite_id = wx.getStorageSync('invite_id')
								if(invite_id == res.data.data.openid){
									wx.redirectTo({
										url: '/pages/index/index'
									})
									return;
								}
								
								app.globalData.userInfo = res.data.data;
								wx.setStorageSync('memberinfo', res.data.data);
								that.setData({
									userinfo:res.data.data
								})
							
              }else{
                wx.showToast({
                  title: res.data.info,
                  icon:'none'
                })
              }
          }
        })
      }
    })
	},
	signup() {
		if(this.data.userinfo.member_code){
			wx.navigateTo({
				url: '/pages/result/result?type=fail'
			})
		
		}else{
			wx.redirectTo({
				url: '/pages/signup/signup'
			})
		}
		
		
	},
	yaoyiyao: function() {
var videoplay3 = wx.createVideoContext('myVideo3')
		videoplay3.play()
		this.setData({
			show: true,
			show4:false,
		})
	},
	ended(e) {
		console.log(this.data)
		this.setData({
			popshow: true
		})
	},
	getPhoneNumber (e) {
		if(this.data.clickable == false){
			return false;
		}
		this.setData({clickable:false})
   	var that = this;
		setTimeout(function(){
			that.setData({clickable:true})
		},2000)
		var userinfo=wx.getStorageSync('memberinfo');
		var that = this;
    var data = {
      openid: userinfo.openid,
      encryptedData:e.detail.encryptedData,
      iv:e.detail.iv
		};
		wx.request({
			url: app.globalData.apiurl+'code2mobile.php', 
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			data: data,
			method:"POST",
			success (res) {
				if(res.data.status == "1"){
					wx.setStorageSync('memberinfo',res.data.data);
					if(res.data.data.member_code){
						wx.navigateTo({
							url: '/pages/result/result?type=fail'
						})
					}else{
						wx.redirectTo({
							url: '/pages/signup/signup'
						})
					}
				}
			}
		})
	}



})
